//
//  YZLSearchPostView.m
//  Car
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSearchPostView.h"
#import "Macro.h"
#import "YZLPostModel.h"
#import "YZLDiscuss.h"
#import "HTTPTOOL.h"
#import "YZLPostCell.h"
#import "MJRefresh.h"

@interface YZLSearchPostView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *postArray;
@property (nonatomic, assign) NSInteger nextPage;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, strong) YZLPostModel *postModel;
@property (nonatomic, strong) UIImageView *searchingImageView;
@property (nonatomic, strong) UIImageView *searchNullImageView; /**< 空的 */

@end

@implementation YZLSearchPostView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 117) style:UITableViewStylePlain];
        [self addSubview:self.tableView];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.rowHeight = (kHeight - 117) / 5;
        
        [self.tableView registerClass:[YZLPostCell class] forCellReuseIdentifier:@"reuse"];
        
        self.postArray = [NSMutableArray array];
        
        self.searchingImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searching"]];
        [self addSubview:self.searchingImageView];
        self.searchingImageView.frame = CGRectMake(0, 0, kWidth, kHeight - 117);

        
    }
    return self;
}

- (void) addFooterWithBodyString:(NSString *)bodyString {
    __unsafe_unretained YZLSearchPostView *newsView = self;
    [self.tableView addFooterWithCallback:^{
        newsView.nextPage += 1;
        newsView.isUpLoading = YES;
        [newsView handelDataWithBodyString:bodyString withPage:newsView.nextPage];
    }];
    self.isUpLoading = NO;
    [self.tableView footerBeginRefreshing];
}

- (void)setSearchString:(NSString *)searchString {
    if (_searchString != searchString) {
        _searchString = searchString;
    }
    [self addFooterWithBodyString:searchString];
}



- (void)handelDataWithBodyString:(NSString *)bodyString withPage:(NSInteger )page {
    NSString *transcodingString = [bodyString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *bodyStr = [NSString stringWithFormat:@"page=%ld&searchContent=%@&", page, transcodingString];
    [HTTPTOOL POSTWithURL:searchPost body:bodyStr bodyStyle:stringStyle httpHead:nil responseStyle:JSON success:^(id result) {
        self.postModel = [YZLPostModel yzlPostModelWithDic:result];
        if (self.isUpLoading == NO) {
            [self.postArray removeAllObjects];
        }
        NSArray *newsListArray = [result objectForKey:@"postList"];
        for (NSDictionary *dic in newsListArray) {
            YZLDiscuss *model = [YZLDiscuss DiscussWithDictionary:dic];
            [self.postArray addObject:model];
        }
        self.postModel.postList = self.postArray;
        [_tableView footerEndRefreshing];
        [_tableView reloadData];
        [self.searchingImageView removeFromSuperview];
        
        if ( self.postModel.postList.count == 0) {
            self.searchNullImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchNull"]];
            [self addSubview:self.searchNullImageView];
            self.searchNullImageView.frame = CGRectMake(0, 0, kWidth, kHeight - 117);
        }

        
    } fail:^(NSError *error) {
        NSLog(@"搜索错误");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.postArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLDiscuss *model = [self.postArray objectAtIndex:indexPath.row];
    
    YZLPostCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.model = model;
    cell.searchString = self.searchString;
    cell.isAll = NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate pushNextViewOfSearchPostWithIndex:indexPath withModel:self.postModel];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
